package com.gzu.finalwork.admin.controller;

import com.gzu.finalwork.admin.service.AdminOrderService;
import com.gzu.finalwork.common.utils.AjaxResult;
import com.gzu.finalwork.common.utils.SensitiveOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 管理员订单管理控制器
 */
@RestController
@RequestMapping("/admin")
@CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
public class AdminOrderController {

    @Autowired
    private AdminOrderService adminOrderService;

    /**
     * 获取所有订单信息（支持筛选、搜索、分页）
     * @param pageNum 页码，默认1
     * @param pageSize 每页大小，默认10
     * @param orderStatus 订单状态筛选（可选）
     * @param keyword 模糊搜索关键词（可选，支持订单号、寄件人、收件人）
     * @param startDate 开始日期（可选，格式：yyyy-MM-dd）
     * @param endDate 结束日期（可选，格式：yyyy-MM-dd）
     * @return 订单列表
     */
    @GetMapping("/orders")
    public Object getAllOrders(@RequestParam(defaultValue = "1") int pageNum,
                              @RequestParam(defaultValue = "10") int pageSize,
                              @RequestParam(required = false) Integer orderStatus,
                              @RequestParam(required = false) String keyword,
                              @RequestParam(required = false) String startDate,
                              @RequestParam(required = false) String endDate) {
        try {
            return adminOrderService.getAllOrders(pageNum, pageSize, orderStatus, keyword, startDate, endDate);
        } catch (Exception e) {
            return AjaxResult.error(500, "获取订单列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据订单ID获取订单详情
     * @param orderId 订单ID
     * @return 订单详情
     */
    @GetMapping("/orders/{orderId}")
    public Object getOrderDetail(@PathVariable Long orderId) {
        try {
            return adminOrderService.getOrderDetail(orderId);
        } catch (Exception e) {
            return AjaxResult.error(500, "获取订单详情失败：" + e.getMessage());
        }
    }

    /**
     * 更新订单状态
     * @param orderId 订单ID
     * @param orderStatus 新的订单状态
     * @param remark 备注（可选）
     * @return 更新结果
     */
    @PutMapping("/orders/{orderId}/status")
    public Object updateOrderStatus(@PathVariable Long orderId,
                                   @RequestParam Integer orderStatus,
                                   @RequestParam(required = false) String remark) {
        try {
            return adminOrderService.updateOrderStatus(orderId, orderStatus, remark);
        } catch (Exception e) {
            return AjaxResult.error(500, "更新订单状态失败：" + e.getMessage());
        }
    }

    /**
     * 获取订单统计信息
     * @return 统计数据
     */
    @GetMapping("/orders/statistics")
    public Object getOrderStatistics() {
        try {
            return adminOrderService.getOrderStatistics();
        } catch (Exception e) {
            return AjaxResult.error(500, "获取订单统计失败：" + e.getMessage());
        }
    }

    /**
     * 删除订单（软删除）
     * @param orderId 订单ID
     * @return 删除结果
     */
    @SensitiveOperation(
        value = "删除订单", 
        operationType = "删除操作"
    )
    @DeleteMapping("/orders/{orderId}")
    public Object deleteOrder(@PathVariable Long orderId) {
        try {
            return adminOrderService.deleteOrder(orderId);
        } catch (Exception e) {
            return AjaxResult.error(500, "删除订单失败：" + e.getMessage());
        }
    }
} 